package com.example.shiwu;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.shiwu.db.model.Items;
import com.example.shiwu.service.ItemsService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ItemInfoDetailsActivity extends AppCompatActivity {
    private TextView ItemInfo;
    private ItemsService itemsService;
    private TextView itemName,itemLocation,itemNum,customerName,constructionUnit,
            itmeOperator,itemLine;
    private String itemNameVal,itemLocationVal,itemNumVal,customerNameVal,constructionUnitVal,
            itmeOperatorVal,deviceTypeVal,itemLineVal;
    private Spinner deviceType;
    private Context myContent;
    private Map<String, Object> showitem;
    private List<Map<String, Object>> listitem;
    private SimpleAdapter myAdapter;
    private TableLayout listView;
    public TextView title_text,title_seting,item_dateil_info_sta;
    public ImageView title_back;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iteminfo_details);
        Intent intent=getIntent();
        String itemId=intent.getStringExtra("itemId");
        title_back = findViewById(R.id.title_back);
        title_text = findViewById(R.id.title_text);
        title_seting = findViewById(R.id.title_setting);
        item_dateil_info_sta = findViewById(R.id.item_dateil_info_sta);
        title_text.setText("项目详情");
        title_seting.setVisibility(View.INVISIBLE);
        title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ItemInfoDetailsActivity.this.finish();
            }
        });

        this.initData(itemId);

        itemsService = new ItemsService();

        ItemInfo = findViewById(R.id.item_details_edit_btn);
        myContent = this;

        ItemInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ItemInfoDetailsActivity.this.finish();
                Intent intent = new Intent(v.getContext(), ItemInfoEditActivity.class);
                //v.getContext().startActivity(intent);
                startActivity(intent);

            }
        });
    }

    public void initData(String id){

        itemsService = new ItemsService();
        List<Items> itemLis = itemsService.getItems(new Items(Integer.parseInt(id)));

        itemName = findViewById(R.id.item_name_detail);
        itemName.setText(itemLis.get(0).getName());
        itemLocation = findViewById(R.id.item_location_details_val);
        itemLocation.setText(itemLis.get(0).getLocation());
        itemNum = findViewById(R.id.item_num_details_val);
        itemNum.setText(itemLis.get(0).getNum());
        customerName= findViewById(R.id.customer_name_details_val);
        customerName.setText(itemLis.get(0).getCustomerName());
        constructionUnit = findViewById(R.id.construction_unit_details_val);
        constructionUnit.setText(itemLis.get(0).getConstructioUnit());
        itmeOperator = findViewById(R.id.itme_operator_details_val);
        itmeOperator.setText(itemLis.get(0).getOperator());
        deviceType= findViewById(R.id.device_type_details_val);
        //deviceType.setAdapter(itemLis.get(0).getDeviceType());
        itemLine = findViewById(R.id.item_line_num_details_val);
        itemLine.setText(itemLis.get(0).getTestLine());
        if(itemLis.get(0).getStatus() == 0){
            item_dateil_info_sta.setVisibility(View.INVISIBLE);
        }

    }

    public Items creatData(TextView itemName,TextView itemLocation,TextView itemNum,TextView customerName,
                           TextView constructionUnit,TextView itmeOperator,Spinner deviceType,TextView itemLine){

        itemNameVal= itemName.getText().toString();
        itemLocationVal= itemLocation.getText().toString();
        itemNumVal= itemNum.getText().toString();
        customerNameVal= customerName.getText().toString();
        constructionUnitVal= constructionUnit.getText().toString();
        itmeOperatorVal= itmeOperator.getText().toString();
        deviceTypeVal = deviceType.getSelectedItem().toString();
        itemLineVal= itemLine.getText().toString();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(new Date());
        System.out.println(currentTime);
        Items items = new Items(itemNameVal,itemLocationVal,itemNumVal,
                0,customerNameVal,constructionUnitVal,itmeOperatorVal,deviceTypeVal,itemLineVal,currentTime,currentTime);
        return items;
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {

            case android.R.id.home:

                finish();

                break;

            default:

                break;

        }

        return super.onOptionsItemSelected(item);
    }


}
